import React, { Component } from 'react'
import VoteItem from '../../../component/voteItem/voteItem'
import request from '../../../util/request'
import Vote from '../../../types/vote'
import {connect} from 'react-redux'

interface Istate{
    voteList:Vote[]
}

interface Iprops{
    initData:(votelist:Vote[]) => void,
    votelist:Vote[]
}

class Home extends Component<Iprops,Istate>{
    state = {
        voteList:[]
    }
    componentDidMount(){
        request.get('/api/votelist').then(res => {
            if(res.data.code === 1){
                this.props.initData(res.data.data)
            }
        })
    }
    render() {
        let {votelist} = this.props;
        return (
            <div>
                {votelist && votelist.map((item:Vote) => <VoteItem key={item.id} item={item}></VoteItem>)}
            </div>
        )
    }
}

let stateMapToProps = (state:any) => {
    return {
        votelist:state.votelist
    }
}

let dispatchMapToProps = (dispatch:any) =>{
    return {
        initData(votelist:Vote[]){
            dispatch({type:'INIT_DATA',votelist})
        }
    }
}


export default connect(stateMapToProps,dispatchMapToProps)(Home)